/**
 * 表单验证工具
 */

/**
 * 验证手机号格式
 * @param {string} phone - 手机号
 * @returns {boolean} - 是否有效
 */
const isValidPhone = (phone) => {
  return /^1[3-9]\d{9}$/.test(phone);
};

/**
 * 验证简历数据
 * @param {object} data - 简历数据
 * @returns {object} - 验证结果，包含 isValid 和 errors
 */
const validateResume = (data) => {
  const errors = {};
  
  // 验证必填字段
  const requiredFields = [
    'linkToken', 'name', 'phone', 'gender', 'weight',
    'canDrink', 'canLivestream', 'acceptNightShift', 'preferredShift', 'weeklyAvailability', 'gameExperience'
  ];
  
  // 字段名映射表
  const fieldMapping = {
    'linkToken': 'link_token',
    'canDrink': 'can_drink',
    'canLivestream': 'can_livestream',
    'acceptNightShift': 'accept_night_shift',
    'preferredShift': 'preferred_shift',
    'weeklyAvailability': 'weekly_availability',
    'gameExperience': 'game_experience'
  };
  
  // 验证必填字段
  requiredFields.forEach(field => {
    // 获取对应的下划线命名字段名
    const dbField = fieldMapping[field] || field;
    
    // 检查前端提交的驼峰命名字段或后端使用的下划线命名字段
    if (!data[field] && !data[dbField]) {
      errors[field] = `${field} 是必填字段`;
    }
  });
  
  // 验证照片
  if (!data.photos || !Array.isArray(data.photos) || data.photos.length === 0) {
    errors.photos = '至少需要上传一张照片';
  } else if (data.photos.length > 5) {
    errors.photos = '最多只能上传5张照片';
  }
  
  // 验证手机号
  if (data.phone && !isValidPhone(data.phone)) {
    errors.phone = '手机号格式不正确';
  }
  
  // 验证体重
  if (data.weight !== undefined) {
    const weight = parseFloat(data.weight);
    if (isNaN(weight) || weight < 30 || weight > 200) {
      errors.weight = '体重必须在30-200kg之间';
    }
  }
  
  // 验证年龄
  if (data.age !== undefined && data.age !== null && data.age !== '') {
    const age = parseInt(data.age);
    if (isNaN(age) || age < 16 || age > 100) {
      errors.age = '年龄必须在16-100岁之间';
    }
  }
  
  // 验证性别
  if (data.gender && !['male', 'female', 'other'].includes(data.gender)) {
    errors.gender = '性别必须是 male、female 或 other';
  }
  
  // 验证可否饮酒
  const canDrink = data.canDrink || data.can_drink;
  if (canDrink && !['yes', 'no'].includes(canDrink)) {
    errors.canDrink = '可否饮酒必须是 yes 或 no';
  }
  
  // 验证能否配合直播
  const canLivestream = data.canLivestream || data.can_livestream;
  if (canLivestream && !['yes', 'no'].includes(canLivestream)) {
    errors.canLivestream = '能否配合直播必须是 yes 或 no';
  }
  
  // 验证是否能接受夜班
  const acceptNightShift = data.acceptNightShift || data.accept_night_shift;
  if (acceptNightShift && !['yes', 'no'].includes(acceptNightShift)) {
    errors.acceptNightShift = '是否能接受夜班必须是 yes 或 no';
  }
  
  // 验证期望工作时间
  const preferredShift = data.preferredShift || data.preferred_shift;
  if (preferredShift && !['day', 'night'].includes(preferredShift)) {
    errors.preferredShift = '期望工作时间必须是 day 或 night';
  }
  
  // 验证游戏熟悉程度
  const gameExperience = data.gameExperience || data.game_experience;
  if (gameExperience && !['none', 'basic', 'familiar', 'expert'].includes(gameExperience)) {
    errors.gameExperience = '游戏熟悉程度必须是 none、basic、familiar 或 expert';
  }
  
  // 验证工作经历
  const workExperience = data.workExperience || data.work_experience;
  if (workExperience && Array.isArray(workExperience)) {
    workExperience.forEach((exp, index) => {
      if (exp && typeof exp === 'object') {
        // 验证日期格式
        if (exp.startDate && !isValidDate(exp.startDate)) {
          errors[`workExperience[${index}].startDate`] = '开始日期格式不正确';
        }
        if (exp.endDate && !isValidDate(exp.endDate)) {
          errors[`workExperience[${index}].endDate`] = '结束日期格式不正确';
        }
      }
    });
  }
  
  return {
    isValid: Object.keys(errors).length === 0,
    errors
  };
};

/**
 * 验证日期格式
 * @param {string|Date} date - 日期字符串或Date对象
 * @returns {boolean} - 是否有效
 */
const isValidDate = (date) => {
  if (!date) return false;
  
  // 如果是字符串，尝试转换为Date对象
  const d = typeof date === 'string' ? new Date(date) : date;
  
  return d instanceof Date && !isNaN(d);
};

module.exports = {
  isValidPhone,
  validateResume,
  isValidDate
}; 